package com.tim.test.leetcode.exam;

import java.util.*;

/**
 * 数库科技
 *  统计字符个数 字符串排序
 *
 * 经验
 *  使用System.out断点，打印疑问值；
 *  多点打印，充当断点作用
 *
 */
public class ShuKu20230609 {

    // 统计字符个数 字符串排序
    public static void main(String[] args) {
        ShuKu20230609 et = new ShuKu20230609();
        Object r = null;
//		r=et.stats("asfd 342 *&%* ");
        r = et.sort("Type.abd5cs#a^d");
        System.out.println(r);
    }
    // 统计字符串中英文、空格、数字、其他字符的数量
    public ArrayList<Integer> stats(String str){
        Integer[] ints = new Integer[4];
        Arrays.fill(ints, 0);
        if(str!=null && str.length()>0){
            str = str.toLowerCase();
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                char aChar = chars[i];
                if(aChar>='a'&& aChar<='z'){
                    ints[0]++;
                }else if(aChar==' '){
                    ints[1]++;
                }else if(aChar>='0'&& aChar<='9'){
                    ints[2]++;
                }else{
                    ints[3]++;
                }
            }
        }

        return new ArrayList<>(Arrays.asList(ints));
    }

    // 字符串中，英文单词倒序
    public String sort(String str){
        if(str==null || str.length()<2){
            return str;
        }
        int size = 26;
        LinkedList<Character>[] lists = new LinkedList[size];
        for (int i = 0; i < size; i++) {
            lists[i] = new LinkedList<>();
        }
        Set<Integer> notWordSet = new HashSet<>();
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if(aChar>='a'&& aChar<='z'){
                int index = aChar-'a';
                lists[index].addLast(aChar);
            }else if(aChar>='A'&& aChar<='Z'){
                int index = aChar-'A';
                lists[index].addLast(aChar);
            }else{
                notWordSet.add(i);
            }
        }

        int listIndex = 0;
        for (int i = 0; i < chars.length; i++) {
            if(!notWordSet.contains(i)){
                LinkedList<Character> list = lists[listIndex];
                while(list.isEmpty() && listIndex<size){
                    listIndex++;
                    list = lists[listIndex];
                }
                chars[i]=list.pop();
            }
        }

        return new String(chars);
    }
}
